Haibo Zhou's site

Mobile Development Articles

What’s new in SwiftUI 2.0 for iOS 14: Part 2?

Continue from Part1. I'd like to introduce ProgressView, Video Player and How to add an AppDelegate in iOS 14 & SwiftUI 2.0 in this article.

ProgressView

ProgressView is remarkable simply in Swift 2.0.

struct ContentView: View {
    @State private var downloadAmount = 0.0
     
    var body: some View {
        VStack {
            // 1
            ProgressView("Download", value: downloadAmount, total: 100)
            // 2
            Button("Increment Download") {
                if downloadAmount < 100 {
                    downloadAmount += 5
                }
            }
        }
    }
}

   

  • ProgressView contains 3 params, 1st is the title of ProgressView, then value means the completed amount of the task, aka current point of download. We declare a @State property downloadAmount above to store the value. And total means the whole range of download, here it is from 0 to 100.
  • Define a Button named Increment Download, the closure underneath will increment the value per user tap the button.

 

text

 

Video Player

Well, video content has been the majority stuff people would like to see from the internet. It could offer the better user experience than texts, articles. In iOS 14, SwiftUI has a new native Video Player component for playing video there are remote or local.

import AVKit
import SwiftUI
 
struct ContentView: View {
     
    var body: some View {
        VideoPlayer(player: AVPlayer(url: URL(string: "https://www.radiantmediaplayer.com/media/big-buck-bunny-360p.mp4")!))
    }
}

 

  • First import AVKit framework which includes VideoPlayer.
  • Then provide some local URL or some remote ones as video source.
  • Do run it in your real device, because iOS simulator sucks on video play, you may get nothing using it.

Or you could try to add a watermark for the video. In below code, the watermark would be put in the bottom-right corner of the video.

var body: some View {
        VideoPlayer(player: AVPlayer(url: URL(string: "https://www.radiantmediaplayer.com/media/big-buck-bunny-360p.mp4")!)) {
            VStack {
                Spacer()
                HStack {
                    Spacer()
                    Text("Watermark")
                        .font(.caption)
                        .foregroundColor(.white)
                        .background(Color.black.opacity(0.7))
                        .clipShape(Capsule())
                }.padding()
            }
        }
    }

 

Add an AppDelegate to a SwiftUI app

In iOS 14, The structure of files become much much simpler. You might find that SceneDelegate and AppDelegate are gone. However, sometimes we need to roll back to old way of AppDelegate work.

Open your xxApp.swift file. Replace your code with following content.

import SwiftUI
 
@main
struct NewIn14App: App {
    // 2
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
     
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
 
// 1
class AppDelegate: NSObject, UIApplicationDelegate {
     
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("Finished launching!")
        return true
    }
}
  1. Create a new Class named AppDelegate, inherits from NSObject and conforms UIApplicationDelegate protocol. Inside that add any standard AppDelegate methods you want to have. E.g. didFinishLaunching
  2. Declare a property wrapper @UIApplicationDelegateAdaptor, AppDelegate.self is your class name. This property wrapper here becomes responsible for creating an app delegate managing its life time and sending information to it automatically.
  3. Command + R to run this app, you should see the “Finished launching!” in the console when the app is launched.

   

Well, that would be all stuff for this time, practice them in your side. Thanks for watching.

Tagged with: